# -*- coding: utf-8 -*-
# 依然
# 每天要开心 \^o^/
from django.contrib.auth.backends import ModelBackend
import re
from .models import Users


class MutiAccountLoginAuth(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):

        try:
             # 校验用户名是否是手机号
            if re.match('^1[35789]\d{9}$', username):
                user = Users.objects.get(phone=username)
            else:
                user = Users.objects.get(username=username)
        except Users.DoesNotExist:
            user = None
        # 校验用户名与密码是否错误
        if user and user.check_password(password):
            return user

